package com.wisdytech.linkdcs.equipment.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.bean.TwoResult;
import com.wisdytech.common.session.UserSession;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.model.CollectionParam;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.equipment.service.IEquipmentModelService;
import com.wisdytech.linkdcs.equipment.service.IEquipmentParamService;
import com.wisdytech.linkdcs.equipment.vo.EquipmentParamVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * 
 * <p>
 * ClassName: EquipmentParamAction
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-26
 * </p>
 */
@Controller
@RequestMapping(value = "/equipment/param")
public class EquipmentParamAction {
    
    @Autowired
    @Qualifier("equipmentParamService")
    private IEquipmentParamService equipmentParamService;
    @Autowired
    @Qualifier("equipmentModelService")
    private IEquipmentModelService equipmentModelService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    
    @RequestMapping("/index")
    public String index(Model model,String modelCode, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        if (StringUtils.isNotBlank(modelCode)){
            model.addAttribute("modelCode",modelCode);
        }
        return "equipment/equipRegistered/modelMange/param";
    }
    
    
    /**
     * 分页条件查询参数管理页面信息
     * 
     * @param emModel 设备类别编号
     * @param modelType 设备类别型号
     * @param filter 分页参数
     * @return 分页查询结果集
     * @throws IOException 
     */
    @RequestMapping(value = "/listEuipmentParam/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentParamVO> listEuipmentParam(String modelCode, String modelType, String paramName, Filter filter) throws IOException {
//    	String emModelTypeStr = new String(emModelType.getBytes("iso8859-1"),"UTF-8");
//    	String paramNameStr = new String(paramName.getBytes("iso8859-1"),"UTF-8");
    	PageInfo<CollectionParam> equipParams= equipmentParamService.listParamByFilter(modelCode, modelType, paramName, filter);
       //转换为前台结果集
       List<CollectionParam> paramlList = equipParams.getList();
       List<EquipmentParamVO> paramVOList=new ArrayList<EquipmentParamVO>();
       for(CollectionParam param:paramlList){
           paramVOList.add(DataTransformUtil.changeParamVO(param));
       }
       return new StandardTableResult<EquipmentParamVO>(equipParams.getTotal(),paramVOList);
    }
    /**
     * <p>Description: 保存参数管理对象</p> 
     * @param param 参数对象
     * @param emModel 设备类别编号
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveCollectionParam/save.mvc")
    @ResponseBody
    public StandardResult saveCollectionParam(CollectionParam param, HttpSession session) {
        SysUser userSession = BaseUtil.getCurrentUser();
        String loginName = userSession.getUsername();
        if(StringUtils.isBlank(param.getFileIgnoreFirst())){
        	param.setFileIgnoreFirst("0");
        }
        
        if (StringUtils.isBlank(param.getPkId())) {
            return equipmentParamService.addCollectionParam(param, loginName);
        }else {
            return equipmentParamService.updateCollectionParam(param, loginName);
        }
    }
    /**
     * 删除设备参数信息
     * @param pkId 参数管理标识id
     * @return 结果对象
     */
    @RequestMapping(value = "/delCollectionParam/del.mvc")
    @ResponseBody
    public StandardResult delCollectionParam(String pkId) {
        return equipmentParamService.delCollectionParam(pkId);
    }
    /**
     * <p>Description: 点击修改时带出对应参数信息</p> 
     * @param emModel 设备类别编号
     * @param pkId 参数唯一id
     * @return 参数管理前台显示对象
     */
    @RequestMapping(value = "/getEuipmentParamById/get.mvc")
    @ResponseBody
    public EquipmentParamVO getEuipmentParamById(String modelCode, String pkId) {
        CollectionParam param = equipmentParamService.getEuipmentParamById(pkId);
        EquipModel model = equipmentModelService.getEuipmentModelByEmModel(modelCode);
        EquipmentParamVO paramVO;
        paramVO = DataTransformUtil.changeParamVO(param, model);
        return paramVO;
    }
    
    /**
     * 
     * <p>Description: 根据设备类别获取设备列表</p>
     * @param modelId 设备类别id
     * @return
     */
    @RequestMapping("/listModelParams/get.mvc")
    @ResponseBody
    public List<EquipmentParamVO> getModelParams(String modelId){
        
        List<CollectionParam>  params= equipmentParamService.listModelParams(modelId);
        
        List<EquipmentParamVO> vos=new ArrayList<>();
        for(CollectionParam param: params){
            vos.add(DataTransformUtil.changeParamVO(param));
        }
        
        return vos;
    }
    
    /**
     * 
     * <p>Description: 根据设备类别获取设备列表</p>
     * @param modelId 设备类别id
     * @return
     */
    @RequestMapping("/listModelParamsByModelCode/get.mvc")
    @ResponseBody
    public List<EquipmentParamVO> getModelParamsByModelCode(String modelCode){
        
        List<CollectionParam>  params= equipmentParamService.listModelParamsByModelCode(modelCode);
        
        List<EquipmentParamVO> vos=new ArrayList<>();
        for(CollectionParam param: params){
            vos.add(DataTransformUtil.changeParamVO(param));
        }
        
        return vos;
    }
    
    /**
     * 
     * <p>Description: 根据设备获取参数列表</p>
     * @param modelId 设备类别id
     * @return
     */
    @RequestMapping("/listEqParams/get.mvc")
    @ResponseBody
    public List<EquipmentParamVO> getEqParams(String eqCode){
        
        List<CollectionParam>  params= equipmentParamService.listEqParams(eqCode);
        
        List<EquipmentParamVO> vos=new ArrayList<>();
        for(CollectionParam param: params){
            vos.add(DataTransformUtil.changeParamVO(param));
        }
        
        return vos;
    }
    
    /***
     * 分页获取参数
     * @param equipCode
     * @return
     */
    @RequestMapping("/listAlaramParams/get.mvc")
    @ResponseBody
    public StandardResult getAlarmParams(String equipCode,String search,Filter filter) {
    	TwoResult<Boolean,List<CollectionParam>> result=new TwoResult<>();
    	
    	if(StringUtils.isEmpty(equipCode)) {
    		result.setResult(false);
    		return StandardResult.getSuccessResult(result);
    	}
    	
    	PageInfo<CollectionParam>  pageInfo=
    			equipmentParamService.listParams(equipCode, search,filter);
    	
    	
    	result.setMessage(pageInfo.getList());
    	
    	if(pageInfo.getPages()>filter.getNum()) {
    		result.setResult(true);
    	}else {
    		result.setResult(false);
    	}
    	
    	
    	return StandardResult.getSuccessResult(result);
    }
    
    
    
    
}
